﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight.Navigation.History
{
    /// <summary>
    /// Responsible for managing an application's history.
    /// </summary>
    public abstract class HistoryManager
    {
        #region Fields

        private HistoryItemCollection _historyItems;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the distinct Uri's, irrespective of timestamp, in the application's
        /// history.
        /// </summary>
        public List<string> DistinctUris
        {
            get
            {
                return (from item in HistoryItems
                        select item.Uri).Distinct().ToList();
            }
        }

        /// <summary>
        /// Represents all items in an application's history.
        /// </summary>
        public HistoryItemCollection HistoryItems
        {
            get
            {
                if (_historyItems == null)
                    _historyItems = new HistoryItemCollection();

                return _historyItems;
            }

            set { _historyItems = value; }
        }

        /// <summary>
        /// Represents all recent history uri's in the application's history.
        /// </summary>
        /// <remarks>
        /// This will return a list of all unique uri's visited since the inception
        /// of the current session of the application.
        /// </remarks>
        public List<string> RecentUris
        {
            get
            {
                return (from item in HistoryItems
                        where item.Timestamp >= SessionStart
                        select item.Uri).Distinct().ToList();
            }
        }

        /// <summary>
        /// Timestamp of when the current session started.
        /// </summary>
        public DateTime SessionStart { get; protected set; }

        #endregion

        #region Methods

        /// <summary>
        /// Adds an item to the application's history.
        /// </summary>
        /// <param name="uri">Uri of the item to add.</param>
        /// <param name="timestamp">Timestamp of the item to add.</param>
        public void Add(string uri, DateTime timestamp)
        {
            this.Add(new HistoryItem() { Uri = uri, Timestamp = timestamp });
        }

        /// <summary>
        /// Adds an item to the application's history.
        /// </summary>
        /// <param name="historyItem">Item to add to history.</param>
        public void Add(HistoryItem historyItem)
        {
            HistoryItems.Add(historyItem);
        }

        /// <summary>
        /// Clears all items in the application's history.
        /// </summary>
        public void Clear()
        {
            HistoryItems.Clear();
        }

        /// <summary>
        /// Loads the items in the application's history.
        /// </summary>
        public void Load()
        {
            this.SessionStart = DateTime.Now;
            this.OnLoad();
        }

        /// <summary>
        /// Loads the items in the application's history.
        /// </summary>
        protected abstract void OnLoad();

        /// <summary>
        /// Saves the items in the application's history.
        /// </summary>
        public abstract void Save();

        #endregion
    }
}
